<template>
	<view>
		<!-- <fui-search-bar placeholder="输入城市名称查询" isLeft fixed @search="search"></fui-search-bar> -->
		<view v-if="CityList.length == 27">
			<fui-index-list :listData="CityList" @click="itemClick">
				<!--解构插槽使用，自定义内容 custom属性 设为true时生效-->
				<!-- <template v-slot:item="{ model, idx, index }">
					<view v-if="idx == 0">
						{{ idx }}- {{ index }} - {{model.text}} 
					</view>
					<view v-else>
						{{ idx }}- {{ index }} - {{model.text}} 
					</view>
				</template> -->
			</fui-index-list>
		</view>
		<view v-else>
			<fui-loading :isFixed="true"></fui-loading>
		</view>
	</view>
</template>

<script>
	import cityListData from "@/script/citylist.js"
	export default {
		data() {
			return {
				custom: true,
				CityList: []
			};
		},
		onLoad() {
			this.getCityData();

		},
		methods: {
			getCityData() {
				this.CityList = cityListData.cityListData
				if (uni.getStorageSync("MyCity")) {
					let MyCity = uni.getStorageSync("MyCity")
					this.CityList[0].data[0] = MyCity;
				} else {
					this.getLocation();
				};

				if (this.CityList.length == 27) {

				}

			},
			itemClick(e) {
				// console.log(e)
				this.CityList[0].data[0] = e
				uni.setStorageSync("MyCity", e);
				this.$forceUpdate();
				this.$common.goSwtPage('/pages/index/index')
			},
			getLocation() {
				var that = this;
				// getFuzzyLocation
				uni.getFuzzyLocation({
					// type: 'wgs84',
					success(res) {
						const latitude = res.latitude
						const longitude = res.longitude
						const speed = res.speed
						const accuracy = res.accuracy
						console.log('getLocation', res) //将获取到的经纬度信息输出到控制台以便检查
						that.setData({ //将获取到的经度、纬度数值分别赋值给本地变量
							latitude: res.latitude,
							longitude: res.longitude
						})
					}
				})
			},
			setData(addJson) {

				// console.log('addJson', addJson);

				// regeo
				uni.request({
					url: 'https://restapi.amap.com/v3/geocode/regeo',
					data: {
						key: '4b984fdef9356e66e5d70e0f3e45730c',
						location: addJson.longitude + ',' + addJson.latitude
					},
					success: res => {
						// console.log('返回用户地理位置',res);

						let MyCity;

						this.CityList.forEach(function(item, index) {
							item.data.forEach(function(itm, idx) {
								if (itm.text == res.data.regeocode.addressComponent.city) {
									MyCity = itm;
								}
							})
						})

						// console.log('MyCity', MyCity)
						uni.setStorageSync("MyCity", MyCity);

						this.CityList[0].data[0] = MyCity;
					},
					fail(err) {
						// console.log(err)
					}
				})
				// end regeo
			}
		}
	}
</script>

<style lang="scss">

</style>